#include "LoginApp.h"

#include "MsgMgr.h"
#include "Log.h"

CLoginApp::CLoginApp()
{
}


CLoginApp::~CLoginApp()
{
}

bool CLoginApp::Init()
{
    bool bFlag = CBaseApp::Init();

    if (bFlag == false)
    {
        return false;
    }

    RmgCmd(cs::ENM_LOGIN_REQ, new cs::LoginReq());

    return true;
}

int CLoginApp::HandleMessage(const comm::Head &pbHead, const google::protobuf::Message &pbMessage)
{
    int iRet = 0;
    int iCmd = CMsgMgr::Instance().GetEnmCmd(pbHead.cmd());
    switch (iCmd)
    {
    case cs::ENM_LOGIN_REQ:
        iRet = HandleCSLogin(pbHead, (const cs::LoginReq &)pbMessage);
        break;
    default:
        LOG_ERR("Can't find handle.Cmd:%d", iCmd);
        iCmd = comm::ENM_RETCODE_UNKNOW_CMD;
        break;
    }

    return iRet;
}

int CLoginApp::HandleCSLogin(const comm::Head &pbHead, const cs::LoginReq &pbReq)
{
    LOG_DBG("welcome lost world!!!");

    cs::LoginRes pbRes;
    pbRes.set_welcomemessage("welcome lost world!!!");
    comm::Head &pbResHead = CMsgMgr::Instance().CreateHeadFromReq(pbHead, cs::ENM_LOGIN_RES, 0);
    CMsgMgr::Instance().SendPacket(pbResHead.socket(), pbRes, comm::ENM_MODULE_LOGIN, cs::ENM_LOGIN_RES);
    return 0;
}

